<template>

  <div  class="m-container-small m-padded-tb-big animated fadeIn">
    <div class="ui container">
      <!--header-->
      <div class="ui top attached segment">
        <div class="ui middle aligned two column grid">
          <div class="column">
            <h3 class="ui teal header">标签</h3>
          </div>
          <div class="right aligned column">
            共 <h2 class="ui orange header m-inline-block m-text-thin"> {{tags.length}} </h2> 个
          </div>
        </div>
      </div>
      <div class="ui attached segment m-padded-tb-large">
        <a style="cursor:pointer" @click="toTag(tag.id)" :class="generateClassName(tag.id)" v-for="tag in tags">
<!--          <div v-if="tag.id===activeTagId">-->
            <span v-text="tag.name"></span><div class="detail">{{tag.count}}</div>
<!--          </div>-->
<!--          <div v-else>-->
<!--            <span v-text="tag.name"></span><div class="detail">{{tag.count}}</div>-->
<!--          </div>-->


        </a>

<!--        <div v-if="tag.id==activeTagId">-->
<!--          <a href="#" class="ui basic left pointing large label m-margin-tb-tiny teal">-->
<!--            <span v-text="tag.name"></span> <div class="detail">{{tag.count}}</div>-->
<!--          </a>-->
<!--        </div>-->
<!--        <div v-else>-->
<!--          <a href="#" class="ui basic left pointing large label m-margin-tb-tiny">-->
<!--            <span v-text="tag.name"></span> <div class="detail">{{tag.count}}</div>-->
<!--          </a>-->
<!--        </div>-->

      </div>

      <div class="ui top attached teal segment">
        <div class="ui padded vertical segment m-padded-tb-large" v-for="blog in blogs">
          <div class="ui middle aligned mobile reversed stackable grid" >
            <div class="eleven wide column">
              <h3 class="ui header" ><a style="cursor:pointer" @click="toBlog(blog.id)" target="_blank" class="m-black">{{blog.title}}</a></h3>
              <p class="m-text">{{blog.description}}</p>
              <div class="ui grid">
                <div class="row">
                  <div class="eleven wide column">
                    <div class="ui mini horizontal link list">
                      <div class="item">
                        <img :src="blog.user.avatar" alt="" class="ui avatar image">
                        <div class="content"><a class="header">{{blog.user.nickname}}</a></div>
                      </div>
                      <div class="item">
                        <i class="calendar icon"></i><span>{{dateFormat(blog.updateTime)}}</span>
                      </div>
                      <div class="item">
                        <i class="eye icon"></i> <span>{{blog.views}}</span>
                      </div>
                    </div>
                  </div>
                  <div class="right aligned five wide column">
                    <a href="#" target="_blank" class="ui teal basic label m-padded-tiny m-text-thin">{{blog.type.name}}</a>
                  </div>
                </div>
                <div class="row">
                  <div class="column">
                    <a class="ui basic left pointing label m-padded-mini m-text-thin teal" v-for="tag in blog.tags">{{tag.name}}</a>
                  </div>
                </div>
              </div>
            </div>

            <div class="five wide column">
              <a style="cursor:pointer" @click="toBlog(blog.id)" target="_blank">
                <img :src="blog.firstPicture" alt="" class="ui rounded image">
              </a>
            </div>

          </div>
        </div>

      </div>

      <!--footer-->
      <div class="ui bottom attached segment">
        <div class="ui middle aligned two column grid">
          <div class="column">
            <div v-if="preBoolean">
              <a @click="toPreType" class="ui mini teal basic button">上一页</a>
            </div>
          </div>
          <div class="right aligned column">
            <div v-if="nexBoolean">
              <a @click="toNexType" class="ui mini teal basic button">下一页</a>
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>

</template>

<script>
import axios from "axios";
import UserBase from "@/views/UserBase";
export default {
  data(){
    return {
      tags: [],
      blogs: [],
      preBoolean: '',
      nexBoolean: '',
      curPage: '',
      activeTagId: ''
    }
  },
  mounted() {
    this.getTag();
    this.$store.commit('setO3');
  },
  methods:{
    toTag(id){
      // alert(id);
      this.$store.commit('setActiveTagId',id);
      var activeTagId = this.$store.state.activeTagId;
      // alert(activeTagId);
      axios.get('http://localhost:7070/tags/'+ activeTagId).then((res)=>{
        console.log(res.data);
        this.tags = res.data.tags;
        this.blogs = res.data.page;
        this.preBoolean = res.data.preBoolean;
        this.nexBoolean = res.data.nexBoolean;
        this.curPage = res.data.curPage;
      })
    },
    toBlog(id){
      this.$store.commit('setBlogId',id);
      this.$router.push({path: '/blog'});
    },
    dateFormat(time) {
      var date=new Date(time);
      var year=date.getFullYear();
      /* 在日期格式中，月份是从0开始的，因此要加0
       * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
       * */
      var month= date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
      var day=date.getDate()<10 ? "0"+date.getDate() : date.getDate();
      var hours=date.getHours()<10 ? "0"+date.getHours() : date.getHours();
      var minutes=date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
      var seconds=date.getSeconds()<10 ? "0"+date.getSeconds() : date.getSeconds();
      // 拼接
      // return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
      return year+"-"+month+"-"+day;
    },
    getTag(){
      axios.get('http://localhost:7070/tags/-1').then((res)=>{
        console.log(res.data);
        this.tags = res.data.tags;
        this.activeTagId = res.data.activeTagId;
        this.blogs = res.data.page;
        this.preBoolean = res.data.preBoolean;
        this.nexBoolean = res.data.nexBoolean;
        this.curPage = res.data.curPage;
        // UserBase.methods.changeN(3);
      })
    },
    toPreType(){
      var url = 'http://localhost:7070/tags/'+(this.activeTagId)+"?page="+(this.curPage-1);
      // alert(url);
      axios.get(url).then((res)=>{
        console.log(res.data);
        this.tags = res.data.tags;
        this.activeTagId = res.data.activeTagId;
        this.blogs = res.data.page;
        this.preBoolean = res.data.preBoolean;
        this.nexBoolean = res.data.nexBoolean;
        this.curPage = this.curPage-1;
      });
    },
    toNexType(){
      var url = 'http://localhost:7070/tags/'+(this.activeTagId)+"?page="+(this.curPage+1);
      // alert(url);
      axios.get(url).then((res)=>{
        console.log(res.data);
        this.tags = res.data.tags;
        this.activeTagId = res.data.activeTagId;
        this.blogs = res.data.page;
        this.preBoolean = res.data.preBoolean;
        this.nexBoolean = res.data.nexBoolean;
        this.curPage = this.curPage+1;
      });
    },
    generateClassName(tagId){
      // var activeTagId = this.$store.state.activeTagId;
      if (tagId === this.activeTagId){
        return 'ui basic left pointing large label m-margin-tb-tiny teal';
      }else {
        return 'ui basic left pointing large label m-margin-tb-tiny';
      }
    }
  }
}
</script>

<style scoped>

</style>